package com.yicong.ants.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cchao.simplelib.core.UiHelper;
import com.yicong.ants.R;

/**
 * 通用控件
 *
 * <attr name="left_label" format="string"/>
 * <attr name="input_hint" format="string"/>
 * <attr name="label_value" format="string"/>
 * <attr name="show_line" format="boolean"/>
 * <attr name="label_type" format="integer"/>
 * <attr name="right_label" format="string"/>
 */
public class GeneralItemView extends RelativeLayout {

    public TextView mLeftLabel;

    public EditText mEditInput;

    public TextView mTextValue;

    public TextView mRightLabel;

    public ImageView ivArrow;
    // 设置控件类型 1:EditText 2:Select 3:label
    public int mType = 1;
    public boolean mRequired = true;
    private View line;

    public GeneralItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public GeneralItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public GeneralItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    /**
     * 初始化控件
     */
    private void initView(Context context, AttributeSet attrs) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.general_item_label, this);
        mLeftLabel = (TextView) rootView.findViewById(R.id.tv_label);
        mEditInput = (EditText) rootView.findViewById(R.id.et_input);
        mTextValue = (TextView) rootView.findViewById(R.id.tv_select);
        mRightLabel = (TextView) rootView.findViewById(R.id.tv_right_text);
        ivArrow = (ImageView) rootView.findViewById(R.id.iv_right_arrow);
        line = rootView.findViewById(R.id.line);
        //自定义属性
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GeneralItemView);
        if (typedArray != null && typedArray.length() > 0) {
            for (int i = 0; i < typedArray.getIndexCount(); i++) {
                int id = typedArray.getIndex(i);
                switch (id) {
                    case R.styleable.GeneralItemView_left_label:
                        setLabel(typedArray.getString(id));
                        break;
                    case R.styleable.GeneralItemView_input_hint:
                        setInputHint(typedArray.getString(id));
                        break;
                    case R.styleable.GeneralItemView_label_value:
                        setValue(typedArray.getString(id));
                        break;
                    case R.styleable.GeneralItemView_show_right_sign:
                        setShowArrow(typedArray.getBoolean(id, true));
                        break;
                    case R.styleable.GeneralItemView_label_type:
                        setViewType(typedArray.getInteger(id, 1));
                        break;
                    case R.styleable.GeneralItemView_right_label:
                        setRightLabel(typedArray.getString(id));
                        break;
                    case R.styleable.GeneralItemView_required:
                        mRequired = typedArray.getBoolean(id, true);
                        break;
                    case R.styleable.GeneralItemView_left_label_text_size:
                        mLeftLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX,typedArray.getDimension(id, 14));
                        break;
                    case R.styleable.GeneralItemView_left_label_text_color:
                        mLeftLabel.setTextColor(typedArray.getColor(id, UiHelper.getColor(R.color.text_333)));
                        break;
                    case R.styleable.GeneralItemView_right_label_text_size:
                        mRightLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX,typedArray.getDimension(id, 14));
                        break;
                    case R.styleable.GeneralItemView_right_label_text_color:
                        mRightLabel.setTextColor(typedArray.getColor(id, UiHelper.getColor(R.color.text_666)));
                        break;
                    case R.styleable.GeneralItemView_show_line:
                        UiHelper.setVisibleElseGone(line, typedArray.getBoolean(id, false));
                        break;
                    default:
                        break;
                }
            }
            typedArray.recycle();
        }
    }

    /**
     * 是否展示箭头
     */
    public void setShowArrow(boolean isShow) {
        ivArrow.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置控件类型 1:EditText 2:Select 3:label
     *
     * @param type
     */
    public void setViewType(int type) {
        mType = type;
        if (type == 1) {
            // edit
            mEditInput.setVisibility(View.VISIBLE);
            mTextValue.setVisibility(View.GONE);
        } else if (type == 3) {
            mTextValue.setVisibility(View.VISIBLE);
            mEditInput.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标签
     */
    public void setLabel(String label) {
        if (!TextUtils.isEmpty(label)) {
            mLeftLabel.setText(label);
        }
    }

    /**
     * 设置Hint
     *
     * @param hint
     */
    public void setInputHint(String hint) {
        if (!TextUtils.isEmpty(hint)) {
            mEditInput.setVisibility(View.VISIBLE);
            mEditInput.setHint(hint);
        }
    }

    /**
     * 获取value
     *
     * @return
     */
    public String getValue() {
        if (mType == 1) {
            return mEditInput.getText().toString();
        }
        return mTextValue.getText().toString();
    }

    public boolean isRequired() {
        return mRequired;
    }

    /**
     * 设置Value
     *
     * @param value
     */
    public void setValue(String value) {
        if (mType == 1) {
            mEditInput.setText(value);
        } else {
            mTextValue.setText(value);
        }
    }

    public TextView getRightLabel() {
        return mRightLabel;
    }

    /**
     * 设置右label
     *
     * @param label
     */
    public void setRightLabel(String label) {
        mRightLabel.setText(label);
        mRightLabel.setVisibility(View.VISIBLE);
    }

    /**
     * 设置右label点击
     *
     * @param onClickListener
     */
    public void setRightLabelClickListener(OnClickListener onClickListener) {
        if (onClickListener == null) {
            mRightLabel.setEnabled(false);
            mRightLabel.setClickable(false);
        } else {
            mRightLabel.setEnabled(true);
            mRightLabel.setClickable(true);
            mRightLabel.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置右label 颜色
     *
     * @param resId
     */
    public void setRightLabelColor(int resId) {
        if (resId != -1) {
            mRightLabel.setTextColor(getResources().getColor(resId));
        }
    }

    public void setRightLabelTextSize(float px) {
        mRightLabel.setTextSize(px);
    }

    /**
     * 是否展示下划线
     */
    public void isShowUnderline(boolean isShow) {
        line.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
}
